<?php
/**
 * 刷新模块
 *
 *
 */
use Workerman\Worker;
use Workerman\Mysql;


require_once( __DIR__.'/conf.php');
require_once IA_LIB . "/class/simple_html_dom.php"; 

class refresh_modules {
    private static $folder_name_conf;
    private static $page_conf;
    private static $module_title_conf;
    private static $sub_page_conf;


    public function entry($param){
        return $this->refresh($param);
    }


    public static function init($g_folder_name_conf,$g_page_conf ,$g_module_title_conf,$g_sub_page_conf) {
        self::$folder_name_conf = $g_folder_name_conf;
        self::$page_conf = $g_page_conf;
        self::$module_title_conf = $g_module_title_conf;
        self::$sub_page_conf = $g_sub_page_conf;
    }

   

    // 获取一个模块下的一行记录
    public function get_single_data( $name ,$code ){
        $class = self::$folder_name_conf[ $code ];
        $file =  $class.'/'.$code.'.php';
        $path = '/modules/'.  $class.'/'.$code ;
        $obj =  $this->get_curl_buttons_info($path);

        $elements = $obj['button_class'];
        $elemtext = $obj['button_text'];
        $rt = (object)[
           "code"  => $code,
           "name"  => $name,
           "elements"  => $elements,
           "elemtext"  => $elemtext,
           "class"  => $class,
           "file"  => $file,
        ];
        return $rt;
    }

    // 记得把hf_mes_modules text改为text
    public function refresh(){
        $module_array = $this->get_module_info();
        $rt =  $this->update_module_info($module_array);
        return array("errcode"=> 0,"errmsg"=>"","rtval"=>$rt);
    }

    private function get_title($param){
        $rt = self::$module_title_conf[ $param ]===NULL? "": self::$module_title_conf[ $param ] ;
    
        return $rt ;
    }

    // 遍历模块结构获得记录数组
    public function get_module_info(){
        $return_arr =[] ;
        foreach (self::$page_conf as $main_title => $main_obj) {
            if(gettype($main_obj)==="array"){
                // continue;
                foreach ( $main_obj as $sub_title =>  $sub_obj) {
                   if(gettype($sub_obj)==="array"){
                        foreach ($sub_obj as $code ) {
                            $name_1 = $this->get_title($main_title); 
                            $name_2 = $this->get_title($sub_title); 
                            $name_3 = $this->get_title($code); 
                            $name =  $name_1 .'/'.  $name_2 .'/'. $name_3 ;


                            $rt =  $this->get_single_data(  $name ,$code );
                            array_push($return_arr, $rt);
                        }
                   }else{
                        $code = $sub_obj;
                        $name_1 = $this->get_title($main_title); 
                        $name_2 = $this->get_title($code); 
                        $name = $name_1  .'/'.  $name_2 ;

                        $rt =  $this->get_single_data(  $name  ,$code  );
                        array_push($return_arr, $rt);
                   }
                }
            }else{
                $code = $main_obj;
                $name_1 = $this->get_title($main_title); 
                $name_2 = $this->get_title($code); 
                $name = $name_1  .'/'.  $name_2 ;
                $rt =  $this->get_single_data(  $name ,$code );
                array_push($return_arr, $rt);
            }

        }

        foreach (self::$sub_page_conf as $code) {
            $name = $this->get_title($code); 
            $rt =  $this->get_single_data(  $name  ,$code  );
            array_push($return_arr, $rt);
        }
        // var_dump( $return_arr,999);
        return $return_arr;
    }

    // 过滤包含多个类元素中包含cps的首个类
    public function class_name_filter($b_class){
        $class_name = '';
        $b_class_arr = preg_split('/\s+/', $b_class);
        foreach ($b_class_arr as $key => $value) {
            if( strstr($value,'cps')!==false ){
                $class_name = $value;
                break;
            }
        }
        return $class_name;
    }

    // 提交数据到 hf_mes_modules
    public function update_module_info($module_array){
        $rt_arr = [];
            $a = sql_query("select * from hf_mes_modules");
            $is_empty_table = false ;
            if(!count( $a )){
                $is_empty_table = true;
            }
            if(!$is_empty_table){
                sql_query("delete from hf_mes_modules");
            }

            foreach ($module_array as $item ) {
                $code = $item->code ;
                $name = $item->name ;  
                $elements = $item->elements ;
                $elemtext = $item->elemtext ;
                $file = $item->file ;
                $class = $item->class ;
                $elemtext = join(",",$elemtext);
                $rt = sql_insert("hf_mes_modules", array('name'=>$name,'code'=>$code,'file'=>$file,'class'=>$class,'elements'=>json_encode($elements,JSON_UNESCAPED_UNICODE),'elemtext'=>$elemtext));
                // echo ($rt)."<br>";
                array_push($rt_arr, $rt);
            }
            return $rt_arr;
    }

    // 模拟浏览器行为得到解析后的代码
    // $path = "./modules/dashbord/moddashborad"
    public function get_curl_str($path,$type='html'){
        $host = $_SERVER['SERVER_NAME'].':'.WEB_PORT; 
       
        if($type==="html"){
            $full_path ="http://". $host . $path .".php" ;
        }else{
            $full_path ="http://". $host . $path.".js.php"  ;
        }
        // var_dump($full_path);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $full_path);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // exec结果为字符串而不是直接输出
        curl_setopt($ch, CURLOPT_HEADER, 0);        // 结果不含头信息
        $response = curl_exec($ch);
        // echo (htmlspecialchars($response)); //debug
        curl_close($ch);

        if($type==="html"){
            return str_get_html($response);
        }else{
            return $response ;
        }
    }


    // 获取一个路径中js,html的cps类数组
    public function get_curl_buttons_info($path){
        $button_class =[] ; $button_text =[] ;
        $html_str = $this->get_curl_str($path,"html");
        $js_str = $this->get_curl_str($path,"js");
        
        // var_dump( $path );

        foreach ($html_str->find('[class*=cps_]') as $buttons ) {
            $b_class = $buttons->class;
            $b_class = $this->class_name_filter($b_class);
            if($b_class===""){
                continue;
            }
            $b_text = trim($buttons->plaintext);
            // echo $module_code." ========= ".$b_class."<br>"; //debug
            array_push($button_class,$b_class);
            array_push($button_text,$b_text);
        }
        
        // js btn
        $js_rt = $this->get_js_btn($js_str,$needle_str="cps");

        foreach ($js_rt as $value) {
            $b_class = $value[0] ;
            $b_text = $value[1] ;
            if(in_array( $b_class , $button_class)===false){
                array_push($button_class,$b_class);
                array_push($button_text,$b_text);
            }
        }
        return [
            "button_class" => $button_class,
            "button_text" => $button_text,
        ];
    }


    // 获取一个路径中js的cps类数组
    public function get_js_btn($js_str,$needle_str="cps"){
        $rt = [] ;
        $arr = explode ( '}',$js_str);
        foreach ($arr as $str) {
            $btn_class = "" ;
            $btn_text = "" ;
            if(strstr($str, $needle_str)!==FALSE){
                $arr = explode('\'', $str);
                foreach ($arr as $str_3) {
                    $arr2 = explode('"', $str_3);

                    foreach ($arr2 as $str_2) {
                        if(strstr($str_2,$needle_str)){
                            $btn_class = $str_2 ;
                        }
                    }
                }

                if($btn_class === "" )continue;
                preg_match('/name.+?[\'|"].+?[\'|"]/', $str, $matches); // name

                foreach ($matches as $str_2) {
                        $arr2 = explode('"', $str_2);
                        foreach ($arr2 as $str_3) {

                            $arr3 = explode('\'', $str_3);
                            foreach ($arr3 as $str_4) {
                                $str_4 = trim($str_4);
                                if($str_4!=="" && $str_4!=="name:"){
                                    $btn_text = $str_4 ;
                                }
                            }
                            
                        }
                }
                array_push($rt, [$btn_class, $btn_text ]);

            }
        }
        return $rt ;
    }

    public static function create(){
        static $instance;
        if (!$instance){
            $instance = new refresh_modules();
        }
        return $instance;
    }

}

refresh_modules::init($folder_name_conf,$page_conf,$module_title_conf,$sub_page_conf);
